home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 774 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.0 KB

  1. From: clamage@Eng.Sun.COM (Steve Clamage)
  2. Message-ID: <4ikkp8$b5u@engnews1.Eng.Sun.COM>
  3. X-Original-Date: 18 Mar 1996 21:32:24 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 19 Mar 96 00:18:48 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Return-Path: <daemon@meeker.UCAR.EDU>
  8. Newsgroups: comp.std.c++
  9. Subject: Re: Constructors and conversion operator
  10. Organization: Sun Microsystems Inc.
  11. References: <314CD29A.3438@cs.tu-berlin.de>
  12. Reply-To: clamage@Eng.Sun.COM
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMU39eOEDnX0m9pzZAQE0oAF/ei1CZIDT/goTRkAGrf3xVvlMYC+hMp0q
  15.     XDwgkcEJyLT9FzOYk+SQykyazyxIma3K
  16.     =oYMh
  17.  
  18. In article 3438@cs.tu-berlin.de, Roman Lechtchinsky
  19. <wolfro@cs.tu-berlin.de> writes:
  20. >
  21. >I have a question about conversion operators and constructors. Consider the 
  22. >following:
  23. >
  24. >class X
  25. >{
  26. >    X( Y );
  27. >};
  28. >
  29. >class Y
  30. >{
  31. >    operator X();
  32. >};
  33.  
  34. A constructor taking a single argument is a type conversion operator from
  35. the argument type to the class type. The arrangement you show is always
  36. going to lead to ambiguity errors. There are two ways to convert a Y
  37. to an X, and neither is preferred over the other. Operator conversion
  38. functions should normally be avoided, partly for this reason, and partly
  39. because they can wind up being used in surprising circumstances. The
  40. compiler might find a conversion that allows erroneous code to compile.
  41.  
  42. >Now the second one:
  43. >
  44. >class A
  45. >{
  46. >        A(B);
  47. >};
  48. >
  49. >class B
  50. >{
  51. >        operator A&();
  52. >};
  53.  
  54. This is the same situation. There is no syntactic difference between
  55. using a reference or an object. Once again, there are two ways to
  56. make an A out of a B, and neither is preferred.
  57.  
  58. ---
  59. Steve Clamage, stephen.clamage@eng.sun.com
  60. ---
  61. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  62. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  63. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  64. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  65. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  66.